package com.lzyyj.gmis.base.model;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ThemeType {
	
	private static Log log = LogFactory.getLog(ThemeType.class);
	
	private ThemeSkin themeSkin;
		
	private String name;
	
	private String defaultStyle;

	private String displayName;

	private Map<String, ThemeStyle> styleMap = new HashMap<String, ThemeStyle>();

	private ThemeType(ThemeSkin themeSkin, String name) {
		this.themeSkin = themeSkin;
		this.name = name;
	}

	/**
	 * 获取指定目录下指定名称的主题类型对象
	 * @param themeSkin
	 * @param file
	 * @param defaultThemeStyle 指定默认主题
	 * @return
	 */
	public static ThemeType getInstance(ThemeSkin themeSkin, File file, String defaultThemeStyle) {
		ThemeType themeType = null;
		
		if (file.exists()&&file.isDirectory()){
			
			// 存在，而且是文件夹，尝试构建风格对象
			themeType = new ThemeType(themeSkin, file.getName());
			for (File themeStyleDir: file.listFiles()){
				if (themeStyleDir.isDirectory()){
					ThemeStyle themeStyle = ThemeStyle.getInstance(themeType, themeStyleDir);
					if (themeStyle!=null){
						themeType.styleMap.put(themeStyle.getName().toLowerCase(), themeStyle);
						if (themeType.defaultStyle==null||themeStyle.getName().equalsIgnoreCase(defaultThemeStyle)){
							// 初始化默认风格选择
							themeType.setDefaultStyle(themeStyle.getName()); 
						}
					}
				}
			}
			if (themeType.styleMap.size()>0){
				log.info("检索到 "+themeType.styleMap.size()+" 个主题风格，默认风格设置为 "+themeType.defaultStyle);
			}else{
				log.info("未检索到有效的主题风格!");
				themeType = null;
			}
		}
		
		return themeType;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public boolean isVald() {
		return styleMap.size()>0;
	}

	public ThemeStyle getThemeStyle(String name) {
		ThemeStyle themeStyle = null;
		
		if (name!=null&&name.length()>0){
			String key = name.toLowerCase();
			if (styleMap.containsKey(key)){
				themeStyle = styleMap.get(key);
			}
		}
		
		return themeStyle;
	}

	public String getPath() {
		String path = "";
		if (themeSkin!=null){
			path = themeSkin.getPath();
			if (path.length()==0){
				path = name;
			}else{
				if (name!=null&&name.length()>0){
					path += "/"+name;
				}
			}
		}
		return path;
	}

	public void add(ThemeStyle themeStyle) {
		styleMap.put(themeStyle.getName(), themeStyle);
	}
	 
	public List<ThemeStyle> themeStyleList(){
		List<ThemeStyle> list = new ArrayList<ThemeStyle>();
		
		for (String themeStyleName:styleMap.keySet()){
			list.add(styleMap.get(themeStyleName));
		}
		
		return list;
	}

	public String getDefaultStyle() {
		return defaultStyle;
	}

	public void setDefaultStyle(String defaultStyle) {
		this.defaultStyle = defaultStyle;
	}

	@Override
	public String toString() {
		return name;
	}

	public ThemeStyle getDefault() {
		ThemeStyle themeStyle = null;
		
		themeStyle = getThemeStyle(defaultStyle);
		
		return themeStyle;
	}

}
